//
//  UIViewController+HDOpinion.m
//  app
//
//  Created by D on 15/6/18.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDOpinion.h"
#import "HDUserInforHttpRequestManger.h"


@implementation HDOpinion

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"意见反馈";
    self.opinion.delegate = self;
    self.opinion.layer.masksToBounds = YES;
    self.opinion.layer.cornerRadius = UF(10);
    self.opinion.layer.borderWidth = 0.5;
    self.opinion.layer.borderColor = [UIColor colorWithRed:191.0/255.0 green:191.0/255.0 blue:191.0/255.0 alpha:1.0].CGColor;
    
    self.submitOpinion.layer.masksToBounds = YES;
    self.submitOpinion.layer.cornerRadius = UF(6);
    
    self.user_opinion = [JKRemoteResult target:self action:@selector(submitOpinion:)];

    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    self.opinion.text = @"";
  
    self.opinion.textColor = [UIColor blackColor];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        //在这里做你响应return键的代码
        if (self.opinion.text.length > 0) {
            [self.opinion resignFirstResponder];
            [self submitToReuslt];
        }else
        {
            UIAlertView * av = [[UIAlertView alloc]initWithTitle:nil message:@"请输入您的宝贵意见" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [self.view addSubview:av];
            [av show];

        }
        
   
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
            
        }
    
    return YES;
}
#pragma mark - 数据处理
-(void)submitOpinion:(JKRemoteResult*)rt
{
    if (self.progress_) {
        [self.progress_ removeFromSuperview];
        self.progress_ = nil;
    }
    
    if (rt.failure) {
        UIAlertView * av = [[UIAlertView alloc]initWithTitle:nil message:@"请检查网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [self.view addSubview:av];
        [av show];
        
        return;
    }
    [self.navigationController popViewControllerAnimated:YES];
            
     
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 提交反馈
- (IBAction)submit:(id)sender {
    
     if ([self.opinion.text isEqualToString:@"   请输入您的宝贵意见"]){
        UIAlertView * av = [[UIAlertView alloc]initWithTitle:nil message:@"请输入您的宝贵意见" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [self.view addSubview:av];
        [av show];
        
    }else
    {
        if (self.opinion.text.length > 0 && self.opinion.text.length < 251) {
            [self.opinion resignFirstResponder];
            [self submitToReuslt];
        }else if (self.opinion.text.length > 251){
            
            UIAlertView * av = [[UIAlertView alloc]initWithTitle:nil message:@"请不要超过250个字~" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [av show];
            
        }else
        {
            UIAlertView * av = [[UIAlertView alloc]initWithTitle:nil message:@"请输入您的宝贵意见" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [av show];
        }
    }
  
}

#pragma mark - 发送请求 
-(void)submitToReuslt
{
    self.progress_ = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:self.progress_];
    [self.view bringSubviewToFront:self.progress_];
    self.progress_.delegate = self;
    self.progress_.labelText = @"请稍后...";
    [self.progress_ show:YES];
    
    
    NSString * uid = [NSString string];
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"uid"]integerValue] != 0) {
        uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"uid"];
    }else
    {
        uid = @"0";
    }
    
    NSDictionary * deviceInfo = @{@"os":@"ios",
                                  @"version":APP_VERSION,
                                  @"mac":KEY_CHAIN_UDID,
                                  @"imei":KEY_CHAIN_UDID};
    
//    NSString * deviceInfo = [NSString stringWithFormat:@"{\"os\":\"ios\",\"version\":\"%@\",\"mac\":\"%@\",\"imei\":\"%@\"}",APP_VERSION,KEY_CHAIN_UDID,KEY_CHAIN_UDID];
    NSDictionary * params = @{@"contents":self.opinion.text,
                              @"deviceinfo":[deviceInfo JSONString],
                              @"uid":uid};
    HDUserInforHttpRequestManger * httpManager = [HDUserInforHttpRequestManger shareUserInforHttpRequestManger];
    [httpManager submitOpinionWithParams:params result:self.user_opinion];
}

@end
